package com.carbon.notice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carbon.notice.dto.CarbonNoticeDTO;
import com.carbon.notice.entity.CarbonNotice;
import com.carbon.notice.mapper.CarbonNoticeMapper;
import com.carbon.notice.service.ICarbonNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carbon.notice.vo.CarbonVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 碳咨询公告表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-06-29
 */
@Service
public class CarbonNoticeServiceImpl extends ServiceImpl<CarbonNoticeMapper, CarbonNotice> implements ICarbonNoticeService {

    @Override
    public List<CarbonNotice> getCarbonNoticeList(CarbonVO vo) {
        List<CarbonNotice> carbonNoticeList = this.baseMapper.getCarbonNoticeList(vo);
        return carbonNoticeList;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCarbonNotice(CarbonNoticeDTO dto) {
        //批量插入附件表
        String appendixList = dto.getAppendixList();
        String[] addrs = appendixList.split(",");
        List<String> addr = Arrays.asList(addrs);
//        List<NoticeAppendix> appendix = new ArrayList<>();


        // TODO: 2021/6/30 批量插入附件表

        CarbonNotice cn = new CarbonNotice();
        BeanUtils.copyProperties(dto, cn);
        return this.save(cn);
    }

    @Override
    public boolean delCarbonNotice(Integer id) {
        boolean update = this.update(Wrappers.<CarbonNotice>update().lambda().set(CarbonNotice::getDelFlag, 1).eq(CarbonNotice::getId, id));
        return update;
    }
}
